import axios from 'axios';
import userUtil from '../js/util/user'
let localData = require('./localData')
/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
/*
* 关闭弹框
* this.layer.closeAll();//疯狂模式，关闭所有层

this.layer.closeAll('dialog'); //关闭信息框

this.layer.closeAll('page');//关闭所有页面层

this.layer.closeAll('iframe');//关闭所有的iframe层

this.layer.closeAll('loading');//关闭加载层

this.layer.closeAll('tips');//关闭所有的tips层
* */
export function get(url,params={}){
    return new Promise((resolve,reject) => {
        axios.get(url,{
          params:params
        })
        .then(response => {
          resolve(response.data);
        })
        .catch(err => {
          reject(err);
        })
    })
}
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
/*var headers={"Content-Type":"multipart/form-data"};*/
export function post(url,data = {},headers){
    //设置token
    axios.defaults.headers = userUtil.setHeaderToken(headers);
    return new Promise((resolve,reject) => {

        let formData = new FormData();

        if(data instanceof FormData){
            formData = data;
        }else{
            Object.keys(data).forEach((key) => {
                if(data[key] && data[key]!=""){
                    formData.append(key, data[key]);
                }
            });
        }


        axios.post(url,formData,headers).then(response => {
//      	console.log(response)
            //this.layer.closeAll('loading');
            if(response.status == 200){
                //新增时，网络避免点击多次请求，2正常，1不能点h
                this.set("notAvailable",2);
                //-2是需要重新登陆的异常
                if(response.data.code == -2){
                    var _this = this;
                    this.layer.msg(response.data.msg);
                    setTimeout(function () {
                        var deviceWidth = document.documentElement.clientWidth;
                        if(deviceWidth>1200){
                            _this.$router.push({path: '/login'});
                        }else if(deviceWidth>800){
                            _this.$router.push({path: '/ipad/login'});
                        }else{
                            _this.$router.push({path: '/mobileLogin'});
                        }
                    },1500);
                    return;
                }
                //-1是报错
                else if(response.data.code == -1){
                    this.layer.msg(response.data.msg);
                }
                //-3验证错误
                else if(response.data.code == -3){
                    this.layer.msg(response.data.msg);
                }
                //0是正常
                else if(response.data.code == 0){
                    resolve(response.data);
                }
            }else{
                this.layer.alert("接口异常")
            }


        },err => {
          reject(err);
//        console.log(err)
        })
    })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url,data = {}){
    return new Promise((resolve,reject) => {
        axios.patch(url,data).then(response => {
          resolve(response.data);
        },err => {
          reject(err)
        })
    })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url,data = {}){
    return new Promise((resolve,reject) => {
        axios.put(url,data).then(response => {
          resolve(response.data);
        },err => {
        	reject(err)
        })
    })
}
